package com.scpii.universal.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.AppContentDto;
import com.scpii.universal.dto.AttributeDto;
import com.scpii.universal.dto.CommentDto;
import com.scpii.universal.dto.PageModuleDetailDto;
import com.scpii.universal.dto.PageModuleSortDto;
import com.scpii.universal.dto.UserInfoDto;
import com.scpii.universal.hibernate.dao.AppAttributeDao;
import com.scpii.universal.hibernate.dao.AppCommentDao;
import com.scpii.universal.hibernate.dao.AppContentDao;
import com.scpii.universal.hibernate.dao.AppDetailPageDao;
import com.scpii.universal.hibernate.dao.AppDetailPageModuleDao;
import com.scpii.universal.hibernate.dao.AppViewContentRalationDao;
import com.scpii.universal.hibernate.dao.AttributeDao;
import com.scpii.universal.hibernate.dao.AttributeListDao;
import com.scpii.universal.hibernate.dao.AttributeValueDao;
import com.scpii.universal.hibernate.dao.CatelogDao;
import com.scpii.universal.hibernate.dao.ContentDao;
import com.scpii.universal.hibernate.dao.ContentImgDao;
import com.scpii.universal.hibernate.dao.PageModuleConDao;
import com.scpii.universal.hibernate.dao.PageModuleSortDao;
import com.scpii.universal.hibernate.entity.AppDetailPageModuleTab;
import com.scpii.universal.hibernate.entity.AppDetailPageTab;
import com.scpii.universal.hibernate.entity.AppViewContentRalationTab;
import com.scpii.universal.hibernate.entity.AttributeListTab;
import com.scpii.universal.hibernate.entity.AttributeTab;
import com.scpii.universal.hibernate.entity.AttributeValueTab;
import com.scpii.universal.hibernate.entity.CatelogTab;
import com.scpii.universal.hibernate.entity.ContentImgTab;
import com.scpii.universal.hibernate.entity.ContentTab;
import com.scpii.universal.hibernate.entity.PageModuleConTab;
import com.scpii.universal.pns.dao.UserActionsDao;
import com.scpii.universal.service.AppContentService;

/**
 * 
 * universalWeb
 * 
 * @author zhangqiang 2012-7-10下午05:21:48
 */
@Service
public class AppContentServiceImpl extends
		BaseServiceImpl<AppContentDto, Integer> implements AppContentService {
	@Resource
	private AppContentDao appContentDao;
	@Resource
	private AppAttributeDao AppAttributeDao;
	@Resource
	private AppCommentDao appCommentDao;
	@Resource
	private ContentImgDao contentImgDao;
	@Resource
	private AttributeValueDao attributeValueDao;
	@Resource
	private ContentDao contentDao;
	@Resource
	private AppDetailPageDao appDetailPageDao;
	@Resource
	private PageModuleConDao pageModuleConDao;
	@Resource
	private CatelogDao catelogDao;
	@Resource
	private PageModuleSortDao pageModuleSortDao;
	@Resource
	private AppDetailPageModuleDao appDetailPageModuleDao;
	@Resource
	private AppViewContentRalationDao appViewContentRalationDao;
	@Resource
	private AttributeDao attributeDao;
	@Resource
	private AttributeListDao attributeListDao;
	@Resource
	private UserActionsDao userActionsDao;
	private Log log = LogFactory.getLog(this.getClass());
	private static SimpleDateFormat sdf = new SimpleDateFormat(GenericConstants.DATE_FORMAT);

	@Resource
	public void setBaseDao(AppContentDao appContentDao) {
		super.setBaseDao(appContentDao);
	}

	/**
	 * 获取内容信息
	 * 
	 * @param id
	 * @return
	 * @author zhangqiang 2012-7-10上午11:05:44
	 */
	@Override
	public List<AppContentDto> getAppContentById(Integer treeId, Integer appId,
			Integer pageNo, Integer pageSize) {

		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.AppContentDto(t1.intContentId,");
		hql.append(" t4.intCateId,");
		hql.append(" t1.varContentName,");
		hql.append(" t2.varEnterpName,");
		hql.append(" t3.varAppName,");
		hql.append(" t4.varCateName,");
		hql.append(" t1.varContentDescript,");
		hql.append(" t1.datAddDatetime,");
		hql.append(" t1.varContentImgUrl,");
		hql.append(" t1.intViewCount,");
		hql.append(" t1.intSortOrder,");
		hql.append(" t1.datUpdateDatetime ) ");
		hql.append(" from ContentTab t1 , ");
		hql.append("  EnterpriseTab t2 , ");
		hql.append("  AppTab t3 , ");
		hql.append("  CatelogTab t4 ");
		hql.append(" where t1.intAppId= t3.intAppId ");
		hql.append(" and  t1.intCateId= t4.intCateId ");
		hql.append(" and t1.intEnterpId= t2.intEnterpId ");
		hql.append(" and t1.intCateId=:treeId ");
		hql.append(" and t1.intAppId=:appId and t1.intDeleteSign=:intDeleteSign");
		hql.append(" order by t1.intSortOrder desc,t1.datUpdateDatetime desc,t1.intContentId desc");

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("appId", appId);
		argsMap.put("treeId", treeId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		
		List<AppContentDto> appContentList = new ArrayList<AppContentDto>();

		appContentList = appContentDao.findByHql(hql.toString(), pageNo, pageSize,argsMap);
		
		//获得内容的分类的顶级系统分类ID
		if (appContentList!=null && appContentList.size()>0) {
			List<AppContentDto> appContListSysCate = new ArrayList<AppContentDto>();
			for (AppContentDto appCont : appContentList) {
				int cateId = appCont.getIntCateId();
				int sysCateId = this.getSuperSystemCatelogId(cateId);
				if (sysCateId>0) {
					appCont.setSysCateId(sysCateId);
				}
				if(sysCateId==0){
					Integer ecOrderCount=userActionsDao.getCountByActionType(new Long(480000000000l+appCont.getIntContentId()), "EC_ORDER_ADD");
					appCont.setIntEcOrderCount(ecOrderCount);
					appCont.setSysCateId(sysCateId);
				}
				appContListSysCate.add(appCont);
				appContentList = appContListSysCate;
			}
		}
		return appContentList;
	}
	
	/**
	 * 根据内容ID，获取内容的最顶级分类属于那个系统分类类型
	 * @param contentId
	 * @return
	 * @author 甘建新
	 * 2012-9-20下午10:32:58
	 */
	private Integer getSuperSystemCatelogId(int catelogId){
		if (catelogId>0) {
			CatelogTab catelog = catelogDao.get(catelogId);
			int parentCatelogId = catelog.getIntParentCateId();
			if (parentCatelogId!=0) {
				parentCatelogId = this.getSuperSystemCatelogId(parentCatelogId);
				return parentCatelogId;
			}else {
				return catelog.getIntType();
			}
		}
		return 0;
	}

	@Override
	public List<ContentTab> getContentIdsByCateId(int cateId, Integer pageNo,
			Integer pageSize) {
		StringBuffer hql = new StringBuffer(
				"SELECT new ContentTab(c.intContentId)");
		hql.append(" from ContentTab c");
		hql.append(" where c.intCateId=:cateId ");
		hql.append(" and c.intDeleteSign=:intDeleteSign");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("cateId", cateId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		return contentDao.findByHql(hql.toString(), pageNo, pageSize, argsMap);
	}

	/**
	 * 根据id的信息，查找parentId等于当前id的数据有多少条记录
	 * 
	 * @param id
	 * @return
	 * @author zhangqiang 2012-7-10上午11:04:15
	 */
	@Override
	public Integer getCountById(String treeId, Integer appId) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.AppContentDto(t1.intContentId) ");
		hql.append(" from ContentTab t1 , ");
		hql.append("  EnterpriseTab t2 , ");
		hql.append("  AppTab t3 , ");
		hql.append("  CatelogTab t4 ");
		hql.append(" where t1.intAppId= t3.intAppId ");
		hql.append(" and  t1.intCateId= t4.intCateId ");
		hql.append(" and t1.intEnterpId= t2.intEnterpId ");
		hql.append(" and t1.intCateId=:treeId ");
		hql.append(" and t1.intAppId=:appId and t1.intDeleteSign=:intDeleteSign");

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("appId", appId);
		argsMap.put("treeId", Integer.parseInt(treeId));

		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);

		List<AppContentDto> catlogList = appContentDao.hqlGetList(
				hql.toString(), argsMap);
		if (catlogList != null) {
			return catlogList.size();
		} else {
			return 0;
		}
	}

	/**
	 * 根据类容Id 返回所有的属性和属性值信息
	 * 
	 * @param intContentId
	 * @return
	 * @author zhangqiang 2012-7-23下午03:04:33
	 */
	@Override
	public List<AttributeDto> getAttribyteById(Integer intContentId) {
		List<AttributeDto> attrDtoList = new ArrayList<AttributeDto>();
		
		if(log.isDebugEnabled()){
			log.debug("详情页面 获取属性值  类容Id：" + intContentId);
		}
		StringBuffer hql = new StringBuffer( "SELECT new com.scpii.universal.dto.AttributeDto(t1.intAttriId,");
		hql.append(" t1.intCateId,");
		hql.append(" t1.varAttriName,");
		hql.append(" t2.intContentId,");
		hql.append(" t2.intAttriValueId ,");
		hql.append(" t2.varAttriValue, ");
		hql.append(" t1.intType ,");
		hql.append(" t1.intInputType, ");
		hql.append(" t2.intAttrListId) ");
		
		hql.append(" from AttributeTab t1 , ");
		hql.append("  AttributeValueTab t2  ");

		hql.append(" where t1.intAttriId= t2.intAttriId ");
		hql.append(" and  t2.intContentId =:intContentId");
		hql.append(" and t1.intDeleteSign=:intDeleteSign");
		hql.append(" and t2.intDeleteSign=:intDeleteSign");

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intContentId", intContentId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);

		attrDtoList = AppAttributeDao.hqlGetList(hql.toString(), argsMap);
		
		/**
		 * 甘建新
		 * 2012-10-10
		 * 获取属性的输入类型
		 */
		if (attrDtoList!=null && attrDtoList.size()>0) {
			List<AttributeDto> attributeDtos = new ArrayList<AttributeDto>();
			for (AttributeDto attributeDto : attrDtoList) {
				int attriId = attributeDto.getIntAttriId();
				int intType = attributeDto.getIntType();
				String attriListId = attributeDto.getIntAttrListId();
				if (attriId>0) {
					AttributeTab attrTab = attributeDao.get(attriId);
					if (attrTab!=null) {
						attributeDto.setIntInputType(attrTab.getIntInputType());
						attributeDto.setIntType(attrTab.getIntType());
					}
					
					//如果属性的类型为地区时
					if (intType == GenericConstants.HOUSE_AREA) {
						int attrListId = 0;
						if (attriListId!=null && attriListId.trim().length()>0) {
							try {
								attrListId = Integer.parseInt(attriListId);
							} catch (Exception e) {
								attrListId =0;
							}
						}
						AttributeListTab attributeListTab = attributeListDao.get(attrListId);
						if (attributeListTab!=null) {
							attributeDto.setVarAttriValue(attributeListTab.getVarAttrListValue());
						}
					}
					attributeDtos.add(attributeDto);
				}
			}
			attrDtoList = attributeDtos;
		}
		return attrDtoList;
	}

	/**
	 * 通过内容ID 获取评论信息
	 * 
	 * @param intContentId
	 * @return
	 * @author zhangqiang 2012-7-23下午04:28:48
	 */
	@Override
	public List<CommentDto> getCommentListById(Integer intContentId) {
		if(log.isDebugEnabled()){
			log.debug("详情页面 获取评论  类容Id：" + intContentId);
		}
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.CommentDto(t1.intCommentId , ");
		hql.append(" t1.intUserId ,");
		hql.append(" t1.intContentId ,");
		hql.append(" t1.varCommentName ,");
		hql.append(" t1.varCommentContent ,");
		hql.append(" t1.datCommentDatetime,");
		hql.append(" t2.varUserLoginName )");
		hql.append(" from CommentTab t1,");
		hql.append("ClientUserTab t2 ");
		hql.append(" where  t1.intUserId= t2.intUserId ");
		hql.append(" and  t1.intContentId=:intContentId");
		hql.append(" and t1.intDeleteSign=:intDeleteSign");
		hql.append(" order by t1.datUpdateDatetime desc");

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intContentId", intContentId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		return appCommentDao.hqlGetList(hql.toString(), argsMap);
	}

	/**
	 * 保存新增的内容详情
	 * 
	 * @param contentTab
	 * @param imgList
	 * @param attList
	 * @author zhangqiang 2012-7-24下午01:57:25
	 */
	@Override
	public Integer saveContent(UserInfoDto userInfo, Integer intCateId,
			ContentTab contentTab, List<ContentImgTab> imgList,
			List<AttributeDto> attList, String webRoot) {

		SimpleDateFormat sdf = new SimpleDateFormat(
				GenericConstants.DATE_FORMAT);
		String nowDate = sdf.format(new Date());
		// 保存详情的基本信息
		ContentTab content = new ContentTab();
		// 应用ID seesion中获取
		content.setIntAppId(userInfo.getAppId());
		// 企业ID seesion中获取
		content.setIntEnterpId(userInfo.getIntEnterpId());
		// 分类ID
		content.setIntCateId(intCateId);
		// 内容名称
		content.setVarContentName(contentTab.getVarContentName());
		// 内容描述
		content.setVarContentDescript(contentTab.getVarContentDescript());
		// 上线时间
		content.setDatAddDatetime(nowDate);
		// 内容缩略图
		content.setVarContentImgUrl(contentTab.getVarContentImgUrl());
		// 删除标示
		content.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
		//action
		content.setIntAction(0);
		//sourceID
		if(contentTab.getIntResourceId()!=null){
			content.setIntResourceId(contentTab.getIntResourceId());
		} else {
			content.setIntResourceId(0l);
		}
		content.setIntViewCount(0);
		// 更新时间
		content.setDatUpdateDatetime(nowDate);
		content.setIntSortOrder(0);
		content.setCommentCount(0);
		content.setFavCount(0);
		content.setJoinCount(0);
		content.setIntPrivilegeUser(0l);
		contentDao.save(content);

		// 保存属性值信息
		saveAttrs(attList, content);
		// 保存图片信息
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		if (imgList != null && imgList.size() > 0) {
			ContentImgTab img = null;
			for (ContentImgTab imgTab : imgList) {
				img = new ContentImgTab();
				img.setIntContentId(content.getIntContentId());
				img.setVarImgUrl(imgTab.getVarImgUrl());
				img.setVarImgName(imgTab.getVarImgName());
				img.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
				img.setVarImgDescrip(imgTab.getVarImgDescrip());
				img.setIntType(imgTab.getIntType());
				img.setIntOrder(imgTab.getIntOrder());
				// 更新时间
				img.setDatUpdateDatetime(sdf1.format(new Date()));
				contentImgDao.save(img);

			}
		}

		// 保存页面信息 提供前端访问
		List<AppDetailPageTab> pageTabList = appDetailPageDao.getList(
				"intDetailPageCateid", this.getParentIntCateId(intCateId).getIntCateId());
		// 判断根节点是够已有数据 如有这不添加
		boolean flag = true;// 默认有
		if (pageTabList != null && pageTabList.size() > 0) {
			for (int i = 0; i < pageTabList.size(); i++) {
				AppDetailPageTab tab = pageTabList.get(i);
				// 其中有一条数据存在时,不用添加
				if (tab.getIntDeleteSign() == GenericConstants.DELETE_SIGN_EXIST) {
					flag = false;
					break;
				}
			}
		}
		if (flag) {
			// 应用详情页面表
			AppDetailPageTab pageTab = new AppDetailPageTab();
			CatelogTab parentCate = this.getParentIntCateId(intCateId);
			// 应用ID
			pageTab.setIntAppId(userInfo.getAppId());
			// 页面类型 父节点的分类id
			pageTab.setIntDetailPageCateid(parentCate.getIntCateId());
			// 页面名称
			pageTab.setVarPageDetailName(parentCate.getVarCateName());
			// 页面类型
			pageTab.setIntPageDetailType(parentCate.getIntType());
			// 页面访问路径
			pageTab.setVarPageUrl(webRoot
					+ "/dataBinding/goContentDetail.action?intContentId=");
			pageTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
			// 更新
			pageTab.setDatUpdateDatetime(nowDate);
			appDetailPageDao.save(pageTab);
			// 保存页面显示循序和内容

			for (int i = 0; i < 6; i++) {
				PageModuleConTab conTab = new PageModuleConTab();
				// 应用详情页面ID
				conTab.setIntPageDetailId(pageTab.getIntPageDetailId());
				conTab.setIntPageModuleId(i + 1);
				conTab.setIntPageModuleOrder((byte) (i + 1));
				conTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
				conTab.setDatUpdateDatetime(nowDate);
				pageModuleConDao.save(conTab);
			}
		}
		return content.getIntContentId();
	}

	private void saveAttrs(List<AttributeDto> attList,ContentTab content) {
		if (attList != null && attList.size() > 0) {
			String nowDate = sdf.format(new Date());
			AttributeValueTab attributeValueTab = null;
			for (AttributeDto attribute : attList) {
				if (attribute.getIntAttriValueId() == null) {
					attributeValueTab = new AttributeValueTab();
					attributeValueTab.setIntContentId(content.getIntContentId());
					attributeValueTab.setIntAttriId(attribute.getIntAttriId());
					attributeValueTab.setVarAttriValue(attribute.getVarAttriValue());
					attributeValueTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
				} else {
					attributeValueTab = attributeValueDao.get(attribute.getIntAttriValueId());
				}
				attributeValueTab.setVarAttriValue(attribute.getVarAttriValue());
				//获得属性值表对应的属性表记录
				AttributeTab attrTab = attributeDao.get(attribute.getIntAttriId());
				if (attrTab!=null) {
					//获取属性的系统属性ID
					int sysAttrId = attrTab.getIntType();
					int inputType = attrTab.getIntInputType();
					String attrValue = attribute.getVarAttriValue();
					if (sysAttrId==GenericConstants.HOUSE_PRICE) {
						String priceRangeId = this.calculatPriceRange(attrValue);
						attributeValueTab.setIntAttrListId(priceRangeId);
					}
					if (inputType == GenericConstants.ATTRIBUTE_INPUT_TYPE_SELECT) {
						String attriListIds = attribute.getIntAttrListId();
						String[] values = new String[2];
						if (attriListIds!=null && attriListIds.trim().length()>0) {
							values = attriListIds.split("#");
						}
						attributeValueTab.setIntAttrListId(values[0]);
						attributeValueTab.setVarAttriValue(values[1]);
					}
				}
				attributeValueTab.setDatMudifyDate(nowDate);
				if (attribute.getIntAttriValueId() == null) {
					attributeValueDao.save(attributeValueTab);
				} else {
					attributeValueDao.update(attributeValueTab);
				}
			}
		}
	}
	
	/**
	 * 返回页面显示的循序和数据页面
	 * 
	 * @return
	 * @author zhangqiang 2012-7-25下午01:18:45
	 */
	@Override
	public List<PageModuleDetailDto> getAppPageSort(List<CatelogTab> cateLogList) {
		if(log.isDebugEnabled()){
			log.debug("获取页面显示循序：");
		}
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.PageModuleSortDto(t1.intPageDetailId , ");
		hql.append(" t1.intAppId ,");
		hql.append(" t1.intPageDetailType ,");
		hql.append(" t2.varPageModuleName )");
		// 应用详情表
		hql.append(" from AppDetailPageTab t1,");
		// 详情模块表
		hql.append("  AppDetailPageModuleTab t2,");
		// 模块关系表
		hql.append("  PageModuleConTab t3 ");
		hql.append(" where t1.intPageDetailId= t3.intPageDetailId ");
		hql.append(" and t2.intPageModuleId= t3.intPageModuleId ");
		// 页面分类ID
		hql.append(" and t1.intDetailPageCateid =:intCateId ");

		hql.append(" and t1.intDeleteSign=:intDeleteSign");
		hql.append(" and t2.intDeleteSign=:intDeleteSign");
		hql.append(" and t3.intDeleteSign=:intDeleteSign");
		hql.append(" order by t3.intPageModuleOrder ");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		// List<PageModuleSortDto>
		List<PageModuleSortDto> temp = new ArrayList<PageModuleSortDto>();
		PageModuleDetailDto detailDto;
		List<PageModuleDetailDto> pageList = new ArrayList<PageModuleDetailDto>();
		// List<PageModuleSortDto> sortList = new
		// ArrayList<PageModuleSortDto>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		for (CatelogTab tab : cateLogList) {
			argsMap.put("intCateId", tab.getIntCateId());
			temp = pageModuleSortDao.hqlGetList(hql.toString(), argsMap);
			detailDto = new PageModuleDetailDto();
			detailDto.setCateTab(tab);
			detailDto.setPageModuleList(temp);
			pageList.add(detailDto);
		}

		return pageList;
	}

	/**
	 * 根据分类ID 获取最顶级的分类ID
	 * 
	 * @param intCateId
	 * @return
	 * @author zhangqiang 2012-7-25下午03:23:39
	 */
	@Override
	public CatelogTab getParentIntCateId(Integer intCateId) {
		// 获取分类列表
		List<CatelogTab> cateList = catelogDao.getSuperiorCatelogs(intCateId);
		for (CatelogTab tab : cateList) {
			if (tab.getIntParentCateId() == 0) {
				return tab;
			}
		}
		return null;
	}

	@Override
	public List<AppDetailPageModuleTab> getAllPageModule() {

		return appDetailPageModuleDao.getAll();
	}

	@Override
	public List<AppDetailPageModuleTab> getPageModuleById(Integer intCateId) {
		if(log.isDebugEnabled()){
			log.debug("获取页面显示的模块和循序：");
		}
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.PageModuleSortDto(t1.intPageDetailId , ");
		hql.append(" t1.intAppId ,");
		hql.append(" t1.intPageDetailType ,");
		hql.append(" t2.varPageUrl ,");
		hql.append(" t2.varPageModuleName )");
		// 应用详情表
		hql.append(" from AppDetailPageTab t1,");
		// 详情模块表
		hql.append("  AppDetailPageModuleTab t2,");
		// 模块关系表
		hql.append("  PageModuleConTab t3 ");
		hql.append(" where t1.intPageDetailId= t3.intPageDetailId ");
		hql.append(" and t2.intPageModuleId= t3.intPageModuleId ");
		// 页面分类ID
		hql.append(" and t1.intDetailPageCateid =:intCateId ");

		hql.append(" and t1.intDeleteSign=:intDeleteSign");
		hql.append(" and t2.intDeleteSign=:intDeleteSign");
		hql.append(" and t3.intDeleteSign=:intDeleteSign");
		hql.append(" order by t3.intPageModuleOrder ");
		Map<String, Object> argsMap = new HashMap<String, Object>();

		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		argsMap.put("intCateId", this.getParentIntCateId(intCateId).getIntCateId());

		return appDetailPageModuleDao.hqlGetList(hql.toString(), argsMap);
	}

	/**
	 * 保存页面模块显示循序
	 * 
	 * @param modules
	 * @param intCateId
	 * @author zhangqiang 2012-8-1下午02:37:16
	 */
	@Override
	public void updatemodule(String modules, Integer intCateId) {

		// 根据分类ID 获取详情页面信息的id
		AppDetailPageTab cateTab = appDetailPageDao.get("intDetailPageCateid",
				intCateId);
		// 根据页面信息的应用详情页面ID 查找页面模块关系
		List<PageModuleConTab> conTabList = pageModuleConDao.getList(
				"intPageDetailId", cateTab.getIntPageDetailId());
		// 删除现有模块关系
		PageModuleConTab conTab = null;
		for (int i = 0; i < conTabList.size(); i++) {
			conTab = conTabList.get(i);
			pageModuleConDao.delete(conTab.getIntPageModuleConId());
		}
		// 添加新关系
		SimpleDateFormat sdf = new SimpleDateFormat(
				GenericConstants.DATE_FORMAT);
		String nowDate = sdf.format(new Date());
		// 页面编辑的娱预览循序
		String[] module = modules.split(",");
		// 页面编辑的娱预览循序
		int length = module.length;

		for (int k = 0; k < length; k++) {
			if (module[k] == null || "".equals(module[k])) {
				continue;
			}
			conTab = new PageModuleConTab();
			// 应用详情页面ID
			conTab.setIntPageDetailId(cateTab.getIntPageDetailId());
			conTab.setIntPageModuleId(Integer.parseInt(module[k]));
			conTab.setIntPageModuleOrder((byte) (k + 1));
			conTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
			conTab.setDatUpdateDatetime(nowDate);
			pageModuleConDao.save(conTab);
		}

	}

	/**
	 * 保存性情页面编辑的信息
	 * 
	 * @param intContentId
	 * @param contentTab
	 * @param imgList
	 * @param attList
	 * @author zhangqiang 2012-8-5下午08:19:54
	 */
	@Override
	public Integer updateContentDetail(Integer intContentId, ContentTab contentTab, List<ContentImgTab> imgList, List<AttributeDto> attList, String deletedImgIds) {
		String nowDate = sdf.format(new Date());
		// 保存详情的基本信息
		ContentTab content = contentDao.get(intContentId);

		// 内容名称
		content.setVarContentName(contentTab.getVarContentName());
		// 内容描述
		content.setVarContentDescript(contentTab.getVarContentDescript());

		// 内容缩略图
		if (contentTab.getVarContentImgUrl() != null && !"".equals(contentTab.getVarContentImgUrl())) {
			content.setVarContentImgUrl(contentTab.getVarContentImgUrl());
		}
		if(contentTab.getIntResourceId()!=null){
			content.setIntResourceId(contentTab.getIntResourceId());
		}

		// 更新时间
		content.setDatUpdateDatetime(nowDate);
		contentDao.update(content);

		// 保存属性值信息
		saveAttrs(attList,content);
		//删除图片
		if (deletedImgIds != null && deletedImgIds.trim().length() > 0) {
			StringBuffer hql = new StringBuffer( "update ContentImgTab c set c.intDeleteSign=1,c.datUpdateDatetime=:datUpdateDatetime");
			hql.append(" where c.intImgId in(" + deletedImgIds+")");
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("datUpdateDatetime", new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format(new Date()));
			contentImgDao.hqlOperation(hql.toString(), argsMap);
		}
		
		// 保存图片信息
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		if (imgList != null && imgList.size() > 0) {
			ContentImgTab img = null;
			for (ContentImgTab imgTab : imgList) {
				if (imgTab.getIntImgId() == null || "".equals(imgTab.getIntImgId())) {
					img = new ContentImgTab();
					img.setIntContentId(intContentId);
					img.setVarImgUrl(imgTab.getVarImgUrl());
					img.setVarImgName(imgTab.getVarImgName());
					img.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
					img.setVarImgDescrip(imgTab.getVarImgDescrip());
					img.setIntType(imgTab.getIntType());
					img.setIntOrder(imgTab.getIntOrder());
					// 更新时间
					img.setDatUpdateDatetime(sdf1.format(new Date()));
					contentImgDao.save(img);
				} else {
					img = contentImgDao.get(imgTab.getIntImgId());
					if (imgTab.getVarImgUrl() != null && !"".equals(imgTab.getVarImgUrl())) {
						img.setVarImgUrl(imgTab.getVarImgUrl());
					}
					if (imgTab.getVarImgName() != null && !"".equals(imgTab.getVarImgName())) {
						img.setVarImgName(imgTab.getVarImgName());
					}
					img.setVarImgDescrip(imgTab.getVarImgDescrip());
					img.setIntOrder(imgTab.getIntOrder());
					// 更新时间
//					img.setDatUpdateDatetime(nowDate);
					img.setIntType(imgTab.getIntType());
					contentImgDao.update(img);
				}
			}
		}
		return intContentId;
	}

	/**
	 * 价格区间
	 * @param price
	 * @return
	 * @author 甘建新
	 * 2012-9-21下午05:25:32
	 */
	private String calculatPriceRange(String price){
		if (price!=null && price.trim().length()>0) {
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("PRICE", price);
			
			StringBuffer funcSql = new StringBuffer("select CALCPRICEAREA(:PRICE) from dual");
			List list = appContentDao.executeFunction(funcSql.toString(), argsMap);
			
			if (list!=null && list.size()==1) {
				String priceRangeId = (String) list.get(0);
				return priceRangeId;
			}
		}
		return "";
	}

	/**
	 * 删除详情信息
	 * 
	 * @param intContentId
	 * @author zhangqiang 2012-8-6上午09:43:27
	 */
	@Override
	public void deleteContentDetail(Integer intContentId, boolean isBinded) {
		// 保存详情的基本信息
		ContentTab content = contentDao.get(intContentId);
		content.setIntDeleteSign(GenericConstants.DELETE_SIGN_DELETED);
		contentDao.update(content);
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		if (isBinded) {
			String[] properties = { "intViewContId" };
			List<AppViewContentRalationTab> lstBindingConts = appViewContentRalationDao .getRelationsByContId(intContentId, properties);
			StringBuffer ids = new StringBuffer("(");
			for (AppViewContentRalationTab appViewContentRalationTab : lstBindingConts) {
				ids.append(appViewContentRalationTab.getIntViewContId()+",");
			}
			ids.deleteCharAt(ids.lastIndexOf(",")).append(")");
			//删除视图内容关联
			String hql = "update AppViewContentRalationTab a set a.intDeleteSign=:intDeleteSign,a.datUpdateDatetime=:datUpdateDatetime "
				+ "where a.intViewContId in" + ids;
			HashMap<String, Object> args = new HashMap<String, Object>();
			args.put("intDeleteSign", GenericConstants.DELETE_SIGN_DELETED);
			args.put("datUpdateDatetime", date);
			appViewContentRalationDao.hqlOperation(hql, args);
			// 删除内容属性绑定
			StringBuffer hqlAttr = new StringBuffer("update BindContentAttributeTab b");
			hqlAttr.append(" set b.intDeleteSign=:intDeleteSign,b.datUpdateDatetime=:datUpdateDatetime");
			hqlAttr.append(" where b.intViewContId in" + ids);
			HashMap<String, Object> argsAttr = new HashMap<String, Object>();
			argsAttr.put("intDeleteSign", GenericConstants.DELETE_SIGN_DELETED);
			argsAttr.put("datUpdateDatetime", date);
			appViewContentRalationDao.hqlOperation(hqlAttr.toString(), argsAttr);
		}
		//删除内容的地图列表
		StringBuffer hqlShop = new StringBuffer("update EnterpShopTab e");
		hqlShop.append(" set e.intDeleteSign=:intDeleteSign,e.datUpdateDatetime=:datUpdateDatetime");
		hqlShop.append(" where e.intContentId=:intContentId");
		HashMap<String, Object> argsShop = new HashMap<String, Object>();
		argsShop.put("intDeleteSign", GenericConstants.DELETE_SIGN_DELETED);
		argsShop.put("datUpdateDatetime", date);
		argsShop.put("intContentId", intContentId);
		contentDao.hqlOperation(hqlShop.toString(), argsShop);
	}
	
	/**
	 * 检测内容是否被页面绑定了
	 * @author taylor
	 * 2012-9-7 上午09:46:49
	 * @param intContentId
	 * @return
	 * boolean
	 */
	@Override
	public boolean checkIfContentBinded(Integer intContentId) {
		Long count = appViewContentRalationDao.getBindingContentCount(intContentId);
		if (count > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 根据类容Id和分类Id 新增已有详情
	 * 
	 * @param intCateId
	 * @param intContents
	 * @author zhangqiang 2012-8-10下午04:28:51
	 */
	@Override
	public void saveCopyContentDetail(Integer intCateId, String intContents) {

		SimpleDateFormat sdf = new SimpleDateFormat(
				GenericConstants.DATE_FORMAT);
		String nowDate = sdf.format(new Date());
		String[] contents = intContents.split(",");
		if (contents != null && !"".equals(contents)) {

			for (String intContentId : contents) {

				if (intContentId != null && !"".equals(intContentId)) {
					// 保存详情的基本信息
					ContentTab content = contentDao.get(Integer
							.parseInt(intContentId));
					ContentTab tab = new ContentTab();
					// 应用ID seesion中获取
					tab.setIntAppId(content.getIntAppId());
					// 企业ID seesion中获取
					tab.setIntEnterpId(content.getIntEnterpId());
					// 分类ID
					tab.setIntCateId(intCateId);
					// 内容名称
					tab.setVarContentName(content.getVarContentName());
					// 内容描述
					tab.setVarContentDescript(content.getVarContentDescript());
					// 上线时间
					tab.setDatAddDatetime(nowDate);
					// 内容缩略图
					tab.setVarContentImgUrl(content.getVarContentImgUrl());
					// 删除标示
					tab.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);

					tab.setIntViewCount(0);
					// 更新时间
					tab.setDatUpdateDatetime(nowDate);
					contentDao.save(tab);
				}
			}

		}
		// 图片资源
		// 评论资源

	}

}
